package _2FindAlgorithm.src.BinarySearch;

public class Demo7_BinarySearch_problem {
    public Demo7_BinarySearch_problem() {
    }

    public static void main(String[] args) {
        int[] nums = new int[]{5, 7, 7, 8, 8, 10};
        int target = 10;
        int x = left(nums, target);
        if (x == -1) {
            System.out.println(x + " -1");
        } else {
            int y = right(nums, target);
            System.out.println(x + " " + y);
        }

    }

    public static int left(int[] nums, int target) {
        int i = 0;
        int j = nums.length - 1;
        int temp = -1;

        while(i <= j) {
            int mid = i + j >>> 1;
            if (nums[mid] < target) {
                i = mid + 1;
            } else if (target < nums[mid]) {
                j = mid - 1;
            } else {
                temp = mid;
                j = mid - 1;
            }
        }

        return temp;
    }

    public static int right(int[] nums, int target) {
        int i = 0;
        int j = nums.length - 1;
        int temp = -1;

        while(i <= j) {
            int mid = i + j >>> 1;
            if (nums[mid] < target) {
                i = mid + 1;
            } else if (target < nums[mid]) {
                j = mid - 1;
            } else {
                temp = mid;
                i = mid + 1;
            }
        }

        return temp;
    }
}
